package red.action;

import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import red.model.ArticleModel;
import red.model.likeModel;
import red.service.TextService;
import red.service.impl.TextServiceImpl;
import red.util.BaseAction;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@SuppressWarnings("ALL")
public class TextAction extends BaseAction{
    private TextService add = new TextServiceImpl();
    private TextService update = new TextServiceImpl();
    private Map<String,Object> result;
    List list=new ArrayList<>();
    List data=new ArrayList<>();
    private int Total;

    public Map<String, Object> getResult() {
        return result;
    }

    public void setResult(Map<String, Object> result) {
        this.result = result;
    }

    //发表文章
    public String addText(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String user_name = req.getParameter("user_name");
        String headline =req.getParameter("headline");
        String column_name = req.getParameter("column_name");
        String content =req.getParameter("content");
        String page = req.getParameter("page");
        String like_number = req.getParameter("like_number");
        String audit =req.getParameter("audit");
        ArticleModel text= new ArticleModel();
        text.setArticle_id(UUID.randomUUID().toString());
        text.setUser_name(user_name);
        text.setHeadline(headline);
        text.setColumn_name(column_name);
        text.setContent(content);
        text.setPage(page);
        text.setLike_number(like_number);
        text.setAudit(audit);
        text.setColumn_id(UUID.randomUUID().toString());
        text.setUser_id(UUID.randomUUID().toString());
        JSONObject list=JSONObject.fromObject(text);
        if(add.addText(text)){
            result.put("data", list);
            result.put("msg", "发表成功！");
        }else{
            result.put("data", null);
            result.put("msg", "发表失败！");
        }
        return "success";
    }

    //按ID查询文章
    public String findTextid(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String text_id = req.getParameter("article_id");
        TextServiceImpl see = new TextServiceImpl();
        list = see.findTextid(text_id);
        if(list.size()>0){
            result.put("data", list);
            result.put("msg", "查看成功");

        }else{
            result.put("msg", "查看失败");
        }
        return "success";
    }

    //浏览量
    public String updatepage(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String text_id = req.getParameter("article_id");
        String page = req.getParameter("page");
        if (update.updatepage(page,text_id)){
            result.put("data", list);
            result.put("msg", "浏览量加一！");
        }else{
            result.put("data", null);
            result.put("msg", "没有加一");
        }
        return "success";
    }

    //点赞量
    public String updatelike_num(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String text_id = req.getParameter("article_id");
        String like_number = req.getParameter("like_number");
        if (update.updatelike_num(like_number,text_id)){
            result.put("data", list);
            result.put("msg", "点赞成功！");
        }else{
            result.put("data", null);
            result.put("msg", "点赞失败！");
        }
        return "success";
    }

    //审核
    public String updateaudit(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String text_id = req.getParameter("article_id");
        String audit = req.getParameter("audit");
        if (update.updateaudit(audit,text_id)){
            result.put("data", list);
            result.put("msg", "审核成功！");
        }else{
            result.put("data", null);
            result.put("msg", "审核失败！");
        }
        return "success";
    }

    //点赞
    public String addlike(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String article_id =req.getParameter("article_id");
        String user_name = req.getParameter("user_name");
        String like_easy = req.getParameter("like_easy");
        likeModel like= new likeModel();
        like.setArticle_id(article_id);
        like.setUser_name(user_name);
        like.setLike_easy(like_easy);
        JSONObject list=JSONObject.fromObject(like);
        if(add.addlike(like)){
            result.put("data", list);
            result.put("msg", "点赞成功！");
        }else{
            result.put("data", null);
            result.put("msg", "点赞失败！");
        }
        return "success";
    }

    //查看是否点赞
    public String findlike(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String text_id = req.getParameter("article_id");
        String user_name = req.getParameter("user_name");
        TextServiceImpl see = new TextServiceImpl();
        list = see.findlike(text_id,user_name);
        if(list.size()>0){
            result.put("data", list);
            result.put("msg", "查看成功");

        }else{
            result.put("data",null);
            result.put("msg", "查看失败");
        }
        return "success";
    }








    public static HttpServletRequest getRequest(){
        return (HttpServletRequest) ServletActionContext.getRequest();
    }
}
